define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fault/index' + location.search,
                   /* add_url: 'fault/add',
                    edit_url: 'fault/edit',*/
                    del_url: 'fault/del',
                    multi_url: 'fault/multi',
                    import_url: 'fault/import',
                    table: 'fault',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'equipment_id', title: __('Equipment_id')},
                        {field: 'hospital.name', title: __('Hospital.name'), operate: 'LIKE'},
                        {field: 'equipment.name', title: __('Equipment.name'), operate: 'LIKE',formatter: Controller.api.formatter.equip},
                        {field: 'equipment.marking', title: __('Equipment.marking'), operate: 'LIKE'},
                        {field: 'equipment.serial', title: __('Equipment.serial'), operate: 'LIKE'},
                        {field: 'equipment.enclosure', title: __('Equipment.enclosure'), operate: 'LIKE'},
                        {field: 'equipment.software_version', title: __('Equipment.software_version'), operate: 'LIKE'},
                        {field: 'department_name', title: __('Department_name'), operate: 'LIKE'},
                        {field: 'problem', title: __('Problem'), operate:false,
                            table: table,
                            events: Table.api.events.operate,
                            buttons:[
                                {
                                    extend: 'data-area=["700px","500px"]',
                                    text: __('Problem'),
                                    name: "problem",
                                    title: __('Problem'),
                                    classname:'btn btn-dialog',
                                    url:'fault/problem',
                                }
                            ],
                            formatter: Table.api.formatter.buttons

                        },
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'contacts', title: __('Contacts'), operate: 'LIKE'},
                        {field: 'contacts_mobile', title: __('Contacts_mobile'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ok',
                                    title:function (row,column) {
                                        var str = '已派单';
                                        if(row["status"]==2){
                                            str = '已完成';
                                        }
                                        return str;
                                    },
                                    text:function (row,column) {
                                        var str = '已派单';
                                        if(row["status"]==2){
                                            str = '已完成';
                                        }
                                        return str;
                                    },
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: function (row,column) {
                                        var str = '确定修改状态为已派单？';
                                        if(row["status"]==2){
                                            str = '确定修改状态为已完成？';
                                        }
                                        return str;
                                    },
                                    url: function (row, column) {
                                        var url = 'fault/operation?ids=' + row.id+'&status=2';
                                        if(row["status"]==2){
                                            url = 'fault/operation?ids=' + row.id+'&status=1';
                                        }
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status==1) {
                                            return false
                                        }
                                        return true;

                                    }
                                },
                            ],
                            formatter:function (value, row, index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row["status"]!=1){
                                    $(table).data("operate-del", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                equip: function (value, row, index) {
                    //这里手动构造URL
                    url = "equipment?id=" +  row.equipment_id;
                    this.url = url;
                    return Table.api.formatter.addtabs.call(this, value, row, index);
                }
            }
        }
    };
    return Controller;
});
